package com.zhonghui.dolo.manager.controller;

import com.zhonghui.dolo.manager.model.vo.*;
import com.zhonghui.dolo.manager.service.GoodsService;
import com.zhonghui.dolo.manager.util.Constans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/mgr/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @ResponseBody
    @RequestMapping("/list")
    public PageResultVo<List<GoodsVo>> list(@RequestBody GoodsReqVo goodsReqVo) {
        int pageSize = goodsReqVo.getLimit() == null ? Constans.DEFAULT_PAGE_SIZE : goodsReqVo.getLimit();
        int pageNum = goodsReqVo.getOffset() != null && goodsReqVo.getLimit() != null
                ? goodsReqVo.getOffset()/goodsReqVo.getLimit() + 1 : 1;
        PageResultVo<List<GoodsVo>> goodsResult = goodsService.queryGoodsPageResult(pageNum, pageSize, goodsReqVo);
        goodsResult.setRes(Constans.RES_SUCCESS);
        return goodsResult;
    }

    @RequestMapping("/toedit")
    public ModelAndView toedit(@RequestParam("goodsId") Integer goodsId, ModelAndView modelAndView) {
        GoodsDetailVo detailVo = goodsService.getGoodsDetail(goodsId);
        if (detailVo == null) {
            modelAndView.setViewName(Constans.ViewName.ERROR);
            return modelAndView;
        }
        modelAndView.addObject("detail", detailVo);
        modelAndView.setViewName(Constans.ViewName.MGR_GOODS_EDIT);
        return modelAndView;
    }

    @RequestMapping("/edit")
    public ModelAndView edit(@ModelAttribute GoodsEditVo goodsEditVo, ModelAndView modelAndView) throws UnsupportedEncodingException {
        // 执行保存方法
        if (goodsEditVo.getGoodsPriceListStr() != null) {
            goodsEditVo.setGoodsCategoryStr(URLDecoder.decode(goodsEditVo.getGoodsCategoryStr(), "utf-8"));
        }
        if (goodsEditVo.getGoodsPriceListStr() != null) {
            goodsEditVo.setGoodsPriceListStr(URLDecoder.decode(goodsEditVo.getGoodsPriceListStr(), "utf-8"));
        }
        int result = goodsService.updateGoods(goodsEditVo);
        if (result > 0) {
            modelAndView.setViewName(Constans.ViewName.MGR_GOODS_LIST);
        } else {
            modelAndView.addObject("result", "修改商品失败");
            return toedit(goodsEditVo.getGoodsId(), modelAndView);
        }
        return modelAndView;
    }

    @RequestMapping("/editAjax")
    @ResponseBody
    public ResultVo<String> editAjax(@RequestBody GoodsEditVo goodsEditVo) {
        ResultVo<String> resultVo = new ResultVo<>();
        resultVo.setRes(Constans.RES_SUCCESS);
        int result = goodsService.updateGoodsV2(goodsEditVo);
        if (result > 0) {
            resultVo.setObj("修改商品成功");
        } else {
            resultVo.setRes(Constans.RES_FAIL);
            resultVo.setObj("修改商品失败");
        }
        return resultVo;
    }

    @RequestMapping("/addAjax")
    @ResponseBody
    public ResultVo<String> addAjax(@RequestBody GoodsEditVo goodsEditVo) {
        ResultVo<String> resultVo = new ResultVo<>();
        resultVo.setRes(Constans.RES_SUCCESS);
        int result = goodsService.addGoods(goodsEditVo);
        if (result > 0) {
            resultVo.setObj("添加商品成功");
        } else {
            resultVo.setRes(Constans.RES_FAIL);
            resultVo.setObj("添加商品失败");
        }
        return resultVo;
    }

    @RequestMapping("/img/upload")
    @ResponseBody
    public ResultVo<String> upload(@RequestParam("file") MultipartFile file, HttpServletRequest req) {
        // 保存文件
        ResultVo<String> resultVo = new ResultVo<>();
        String foldPath = req.getServletContext().getRealPath("/static/img");
        String fileName = UUID.randomUUID() + ".jpg";
        File out = new File(foldPath + File.separator + fileName);
        try {
            FileCopyUtils.copy(file.getBytes(), out);
            resultVo.setRes(Constans.RES_SUCCESS);
            resultVo.setObj("http://localhost:8080/img/" + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultVo;
    }
}
